﻿using Sui.Domain.Repository;
using Dotnet.Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sui.Domain.App
{
    public class ZfbApp : AppBase
    {
        private IZfbRepository service = new ZfbRepository(conn);

        private ZfbEntity GetEntity(DataRow dr)
        {
            ZfbEntity entity = new ZfbEntity();
            entity.id = dr[0].ToString().Trim(); //dr["交易号"].ToString().Trim();
            foreach(System.Reflection.PropertyInfo item in entity.GetType().GetProperties())
            {
                string name = item.Name;
                var columns=dr.Table.Columns.Cast<DataColumn>().ToList().FirstOrDefault((t)=> {
                    if (t.ColumnName.Replace("/","").Contains(name))
                        return true;
                    return false;
                });
                if (columns!=null)
                {
                    object value = dr[columns.ColumnName];
                    if (item.PropertyType.Name == "String")
                    {
                        item.SetValue(entity, value.ToString());
                    }
                    else if (item.PropertyType.Name == "Decimal")
                    {
                        decimal je = 0;
                        bool ret = Decimal.TryParse(value.ToString(), out je);
                        item.SetValue(entity, je);
                    }
                    else if (item.PropertyType.Name == "DateTime")
                    {
                        DateTime dt = DateTime.Now;
                        bool ret = DateTime.TryParse(value.ToString(), out dt);
                        if (ret == true)
                        {
                            item.SetValue(entity, dt);
                        }
                    }
                    else
                    {

                    }
                }
            }
            return entity;
        }
        private ZfbEntity GetEntityWx(DataRow dr)
        {
            ZfbEntity entity = new ZfbEntity();
            entity.id = dr["交易单号"].ToString().Trim(); //dr["交易号"].ToString().Trim();
            foreach (System.Reflection.PropertyInfo item in entity.GetType().GetProperties())
            {
                if (dr.Table.Columns.Contains(item.Name))
                {
                    object value = dr[item.Name];
                    if (item.PropertyType.Name == "String")
                    {
                        item.SetValue(entity, value.ToString());
                    }
                    else if (item.PropertyType.Name == "Decimal")
                    {
                        decimal je = 0;
                        bool ret = Decimal.TryParse(value.ToString(), out je);
                        item.SetValue(entity, je);
                    }
                    else if (item.PropertyType.Name == "DateTime")
                    {
                        DateTime dt = DateTime.Now;
                        bool ret = DateTime.TryParse(value.ToString(), out dt);
                        if (ret == true)
                        {
                            item.SetValue(entity, dt);
                        }
                    }
                }
                else
                {
                    if (item.Name == "类型")
                    {
                        item.SetValue(entity, dr["交易类型"]);
                    }
                    else if (item.Name == "创建时间")
                    {
                        DateTime dt = DateTime.Now;
                        bool ret = DateTime.TryParse(dr["交易时间"].ToString(), out dt);
                        if (ret == true)
                        {
                            item.SetValue(entity, dt);
                        }
                        //item.SetValue
                    }
                    else if (item.Name == "商品名称")
                    {
                        item.SetValue(entity, dr["商品"]);
                    }
                    else if (item.Name == "交易状态")
                    {
                        item.SetValue(entity, dr["当前状态"]);
                    }
                    else if (item.Name == "资金状态")
                    {
                        item.SetValue(entity, dr["支付方式"]);
                    }
                    else if (item.Name == "金额")
                    {
                        decimal je = 0;
                        bool ret = Decimal.TryParse(dr["金额(元)"].ToString().Trim('￥'), out je);
                        item.SetValue(entity, je);
                    }
                    else if (item.Name == "收支")
                    {
                        item.SetValue(entity, dr["收/支"]);
                    }
                }
            }
            entity.来源地 = "微信";
            return entity;
        }
        public void Save(DataTable dt, Action<string> fn)
        {
            if (dt == null)
                return;
            if (dt.Rows.Count == 0)
                return;
            int sum = 0, addSum = 0, updateSum = 0;
            Stopwatch sw = Stopwatch.StartNew();
            foreach (DataRow dr in dt.Rows)
            {
                sum ++;
                ZfbEntity entity = GetEntity(dr);
                try
                {             
                    string id = entity.id.Trim();
                    ZfbEntity temp = service.FindEntity(id);
                    //string id1 = temp.id.Substring(0, 25);
                    //string strSql = $"select *from Zfb where id like '%{entity.id}%' and 订单号='{entity.订单号}'";
                    //List<ZfbEntity> list = service.FindList(strSql);
                    if (temp == null)
                    {
                        entity.creator_time = DateTime.Now;
                        int ret=service.Insert(entity);
                        entity.年 = entity.创建时间.Year;
                        entity.月 = entity.创建时间.Month;
                        entity.日 = entity.创建时间.Day;
                        entity.时 = entity.创建时间.Hour;
                        entity.分 = entity.创建时间.Minute;
                        addSum += ret;
                        fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s 添加:{addSum} id:{id} 剩余:{dt.Rows.Count - sum}");
                    }
                    else
                    {
                        temp.modify_time = DateTime.Now;
                        temp.付款时间 = entity.付款时间;
                        temp.修改时间 = entity.修改时间;
                        temp.收支 = entity.收支;
                        temp.交易状态 = entity.交易状态;
                        temp.资金状态 = entity.资金状态;
                        temp.金额 = entity.金额;

                        temp.来源地 = entity.来源地;
                        temp.类型 = entity.类型;
                        temp.交易对方 = entity.交易对方;
                        temp.商品名称 = entity.商品名称;
                        temp.服务费 = entity.服务费;
                        temp.备注 = entity.备注;

                        temp.年 = entity.创建时间.Year;
                        temp.月 = entity.创建时间.Month;
                        temp.日 = entity.创建时间.Day;
                        temp.时 = entity.创建时间.Hour;
                        temp.分 = entity.创建时间.Minute;
                        int ret = service.Update(temp);
                        updateSum += ret;
                        fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s 更新:{updateSum} id:{id} 剩余:{dt.Rows.Count - sum}");
                    }

                }
                catch (Exception ex)
                {
                    fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s5 添加:{addSum} 更新:{updateSum} 剩余:{dt.Rows.Count - sum} 异常:{ex.Message}\r\n数据:{entity.ToJson()}");
                }
            }
        }

        public void SaveWx(DataTable dt, Action<string> fn)
        {
            if (dt == null)
                return;
            if (dt.Rows.Count == 0)
                return;
            int sum = 0, addSum = 0, updateSum = 0;
            Stopwatch sw = Stopwatch.StartNew();
            foreach (DataRow dr in dt.Rows)
            {
                sum++;
                ZfbEntity entity = GetEntityWx(dr);
                try
                {
                    string id = entity.id.Trim();
                    ZfbEntity temp = service.FindEntity(id);
                    //string id1 = temp.id.Substring(0, 25);
                    //string strSql = $"select *from Zfb where id like '%{entity.id}%' and 订单号='{entity.订单号}'";
                    //List<ZfbEntity> list = service.FindList(strSql);
                    if (temp == null)
                    {
                        entity.creator_time = DateTime.Now;
                        entity.年 = entity.创建时间.Year;
                        entity.月 = entity.创建时间.Month;
                        entity.日 = entity.创建时间.Day;
                        entity.时 = entity.创建时间.Hour;
                        entity.分 = entity.创建时间.Minute;
                        int ret = service.Insert(entity);
                        addSum += ret;
                        fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s 添加:{addSum} id:{id} 剩余:{dt.Rows.Count - sum}");
                    }
                    else
                    {
                        temp.modify_time = DateTime.Now;
                        temp.付款时间 = entity.付款时间;
                        temp.修改时间 = entity.修改时间;
                        temp.收支 = entity.收支;
                        temp.交易状态 = entity.交易状态;
                        temp.资金状态 = entity.资金状态;
                        temp.金额 = entity.金额;

                        temp.来源地 = entity.来源地;
                        temp.类型 = entity.类型;
                        temp.交易对方 = entity.交易对方;
                        temp.商品名称 = entity.商品名称;
                        temp.服务费 = entity.服务费;
                        temp.备注 = entity.备注;
                        temp.年 = entity.创建时间.Year;
                        temp.月 = entity.创建时间.Month;
                        temp.日 = entity.创建时间.Day;
                        temp.时 = entity.创建时间.Hour;
                        temp.分 = entity.创建时间.Minute;
                        int ret = service.Update(temp);
                        updateSum += ret;
                        fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s 更新:{updateSum} id:{id} 剩余:{dt.Rows.Count - sum}");
                    }

                }
                catch (Exception ex)
                {
                    fn($"总条数:{dt.Rows.Count} 总耗时:{sw.Elapsed.TotalSeconds.ToString("N2")}s5 添加:{addSum} 更新:{updateSum} 剩余:{dt.Rows.Count - sum} 异常:{ex.Message}\r\n数据:{entity.ToJson()}");
                }
            }
        }

        /// <summary>
        /// 存储数据到表
        /// </summary>
        /// <param name="dr">一行数据</param>
        /// <param name="saveType">1:Insert,2:Update</param>
        /// <returns>存储返回条数</returns>
        public int Save(DataRow dr, out string saveType, string lrd="")
        {
            ZfbEntity entity = GetEntity(dr);
            string id = entity.id.Trim();
            ZfbEntity temp = service.FindEntity(id);
            entity.年 = entity.创建时间.Year;
            entity.月 = entity.创建时间.Month;
            entity.日 = entity.创建时间.Day;
            entity.时 = entity.创建时间.Hour;
            entity.分 = entity.创建时间.Minute;
            int ret = 0;
            if (temp == null)//不存在记录
            {
                entity.creator_time = DateTime.Now;
                entity.来源地 = $"{lrd}{entity.来源地}";
                ret = service.Insert(entity);
                saveType = "Insert";
            }
            else
            {
                temp.modify_time = DateTime.Now;
                temp.付款时间 = entity.付款时间;
                temp.修改时间 = entity.修改时间;
                temp.收支 = entity.收支;
                temp.交易状态 = entity.交易状态;
                temp.资金状态 = entity.资金状态;
                temp.金额 = entity.金额;

                temp.来源地 = $"{lrd}{entity.来源地}";
                temp.类型 = entity.类型;
                temp.交易对方 = entity.交易对方;
                temp.商品名称 = entity.商品名称;
                temp.服务费 = entity.服务费;
                temp.备注 = entity.备注;
                ret = service.Update(temp);
                saveType = "Update";
            }
            return ret;
        }
    }
}
